﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using AutoMapper;

namespace Data {
    public class PhieuThuTienRepository {
        /// <summary>
        /// tạo ra mã phiếu thu tiền
        /// </summary>
        /// <returns></returns>
        public string CreateMaPhieuThu() {
            //giay gia tri index ra
            QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();
            //indentify ID
            var maxID = (from max in context.GetIndentity("PHIEUTHUTIEN")
                         select max.CurrentIndentity).FirstOrDefault();
            if (maxID == 1)
                maxID = 0;

            return "PT" + (maxID + 1);
        }

        /// <summary>
        /// them 1 phieu thu tien moi
        /// </summary>
        /// <param name="value"></param>
        public bool InsertNewPhieuThu(PhieuThuTienDTO value) {
            try {
                QuanLyNhaSachEntities context = new QuanLyNhaSachEntities();
                Mapper.CreateMap<PhieuThuTienDTO, PHIEUTHUTIEN>();                
                PHIEUTHUTIEN phieuThu = new PHIEUTHUTIEN();
                phieuThu = Mapper.Map<PhieuThuTienDTO, PHIEUTHUTIEN>(value);

                context.PhieuThuTienList.AddObject(phieuThu);
                context.SaveChanges();

                return true;
            }
            catch {
                return false;
            }
        }
    }
}
